//
//  LGForumDetailViewController.m
//  ligenyun
//
//  Created by Mac on 2018/4/10.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGForumDetailViewController.h"
#import "STPhotoUrlImageBrowerController.h"
#import "LGForumDetailTableViewCell.h"
#import "LGForumReplayDetailViewController.h"
@interface LGForumDetailViewController ()<STPhotoUrlImageBrowerControllerDelegate>
@property(nonatomic, weak) STButton                     *iconButton;
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) NSMutableArray                     *buttonsArray;
@end

@implementation LGForumDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"帖子详情";
    self.dataSouce = @[@"8",@"9",@"10",
                       @"11",@"12",@"13"];
    [self configSubView];
    [self configFooterAndBottomView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 30)];
    header.backgroundColor = [UIColor whiteColor];
    
    STButton * iconButton = [[STButton alloc] initWithFrame:CGRectMake(15, 10, 60, 60)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:30
                                           backgroundColor:nil
                                           backgroundImage:[UIImage imageNamed:@"1"]
                                                     image:nil];
    [header addSubview:iconButton];
    
    STLabel * nameLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.right + 10, iconButton.top + 10, 200, 20)
                                                     text:@"名字"
                                                textColor:FirstTextColor
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:nameLable];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.left, iconButton.bottom + 10, UIScreenWidth - 30, 20)
                                                     text:@"置顶:祭祀时华夏电力的一部分"
                                               textColor:FirstTextColor
                                                    font:16
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.left, titleLable.bottom + 10, UIScreenWidth - 30, 20)
                                                     text:@"     祭祀时华夏电力的一部分大家开始了打算来得及卡死的大健康来得及阿喀琉斯电视剧阿拉丁"
                                                textColor:SecendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [header addSubview:desLable];
    [NSString lableAutoAdjustheightWithLabel:desLable];
    
    CGFloat buttonBootom = 0 ;
    NSArray * imageNameArray = self.dataSouce;
    self.buttonsArray = [NSMutableArray new];
    CGFloat buttonWith = (UIScreenWidth - 30 - 20)/3;
    for (NSInteger i = 0; i < imageNameArray.count; i ++) {
        NSString * imageName = imageNameArray[i];
        STButton * imageButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, buttonWith, buttonWith)
                                                         title:nil
                                                    titleColor:nil
                                                     titleFont:0
                                                  cornerRadius:0
                                               backgroundColor:nil
                                               backgroundImage:nil
                                                         image:[UIImage imageNamed:imageName]];
        [header addSubview:imageButton];
        [imageButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelctedImageButton:sender];
        }];
        imageButton.tag = 10000 + i;
        imageButton.top = desLable.bottom + 20 + (i/3)* buttonWith + 10 * (i/3);
        imageButton.left = 15 + 10 * (i%3) + buttonWith * (i%3);
        imageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [header addSubview:imageButton];
        [self.buttonsArray addObject:imageButton];
        buttonBootom = imageButton.bottom;
    }
#pragma mark --底部
    NSDate * date = [NSDate st_localCurrentDate];
    STLabel * timeLable = [[STLabel alloc] initWithFrame:CGRectMake(15, buttonBootom + 20, 100, 44)
                                                     text:date.st_dynamicFormater
                                                textColor:SecendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [NSString lableAutoAdjustWitdhWithLabel:timeLable];
    [header addSubview:timeLable];
    
    STLabel * typeLable = [[STLabel alloc] initWithFrame:CGRectMake(20 + timeLable.right, buttonBootom + 20, 70, 30)
                                                    text:@"帮助中心"
                                               textColor:[UIColor whiteColor]
                                                    font:14
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentCenter];
    typeLable.backgroundColor = TM_redColor;
    typeLable.layer.cornerRadius = 4;
    typeLable.clipsToBounds = YES;
    typeLable.centerY = timeLable.centerY;
    [header addSubview:typeLable];
    
    STButton* goodButton = [[STButton alloc] initWithFrame:CGRectMake(typeLable.right + 30, buttonBootom + 20, 60, 44)
                                                     title:@"123"
                                                titleColor:SecendTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"已点赞"]];
    goodButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    goodButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [header addSubview:goodButton];
    
    
    STButton *  commentButton = [[STButton alloc] initWithFrame:CGRectMake(goodButton.right , buttonBootom + 20, 80, 44)
                                                          title:@"13323"
                                                     titleColor:SecendTextColor
                                                      titleFont:14
                                                   cornerRadius:0
                                                backgroundColor:nil
                                                backgroundImage:nil
                                                          image:[UIImage imageNamed:@"论坛_消息"]];
    commentButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [header addSubview:commentButton];
    
//    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, commentButton.bottom + 20, UIScreenWidth, 1)];
//    line.backgroundColor = TM_lineColor;
//    [header addSubview:line];
    
    header.height = commentButton.bottom;
    self.tableView.tableHeaderView = header;
}
- (void)configFooterAndBottomView{
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    self.tableView.tableFooterView = footer;
    __weak typeof(self) weakSelf =  self;
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    bootomView.top = [TMUtils tabBarTop];
    bootomView.backgroundColor = [UIColor whiteColor];
    [bootomView st_showTopShadow];
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 1, UIScreenWidth, 48)
                                                     title:@"说点什么吧"
                                                titleColor:SecendTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [bootomView addSubview:buyButton];
    [buyButton setClicAction:^(UIButton *sender) {
        LGCultureCommentPresentController * vc = [LGCultureCommentPresentController new];
        [vc setOnSelctedSendHandle:^(NSString *text) {
            
        }];
        [weakSelf presentViewController:vc animated:NO completion:nil];
    }];
    [self.view addSubview:bootomView];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [LGForumDetailTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    LGForumDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[LGForumDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    __weak typeof(self) weakSelf =  self;
    [cell.peopleButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedMoreReplayButtonWithIndexPath:indexPath];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedImageButton:(id)sender{
    self.iconButton = sender;
    NSMutableArray * models = [NSMutableArray new];
    for (NSString * name in self.dataSouce) {
        STUrlPhotoModel * model = [STUrlPhotoModel  new];
        model.originImage = [UIImage imageNamed:name];
        [models addObject:model];
    }
    NSInteger index = self.iconButton.tag - 10000;
    STPhotoUrlImageBrowerController * vc = [[STPhotoUrlImageBrowerController alloc] initWithArray:models curentIndex:index];
    vc.shouldHideBottomView = YES;
    vc.STPhotoUrlImageBrowerControllerdelegate = self;
    [self presentViewController:vc animated:NO completion:nil];
}
- (void)onSelctedMoreReplayButtonWithIndexPath:(NSIndexPath*)indexPath{
    [self.navigationController pushViewController:[LGForumReplayDetailViewController new] animated:YES];
}
#pragma mark --STPhotoUrlImageBrowerControllerDelegate
- (UIView*)STPhotoSystemBrowserControllerDidScrollToIndexpath:(NSIndexPath*)indexPath model:(STUrlPhotoModel*)model{
    NSInteger index = indexPath.row;
    return   self.buttonsArray[index];
}
@end
